package com.zuoye.T2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/7 17:22
 * @description
 */
public class TextProcessor {

    public int getWordNum(String fileName){
        try {
            FileInputStream fis = new FileInputStream(fileName);
            byte[] buff = new byte[1024];

            //实际读入的字节数
            int i =  fis.read(buff);

            String.valueOf(i);
            String str = new String(buff, 0, i);
            char[] chars = str.toCharArray();
            int count = 0;
            for (char c : chars) {
                if (c==' '){
                    count++;
                }
            }
            return (count+1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void getWordNumFile(String formfileName, String word){
        try {
            FileInputStream fis = new FileInputStream(formfileName);
            byte[] buff = new byte[1024];

            //实际读入的字节数
            int i =  fis.read(buff);

            String.valueOf(i);
            String str = new String(buff, 0, i);
//            System.out.println("str = " + str);
            int count = 0;
            while (true) {
                int index = str.indexOf("asd");
                if (index==-1){
                    break;
                }else {
                    count++;
                }
                str = str.substring(index + word.length());
//                System.out.println("str = " + str);
            }

            System.out.println("文件中含有"+word+"的个数为"+count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        getWordNumFile("D:\\file\\test\\0001.txt","asd");
    }


}
